package com.tanhua.server.controller;

/**
 * @Author: cl
 * @Date: 2022/5/2 - 05 - 02
 * @Description:
 */

import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.User;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.PhoneVo;
import com.tanhua.model.vo.SettingsVo;

import java.util.Map;

/**
 * 通用设置模块
 */
@RestController
@RequestMapping("/users")
public class SettingsController {

    //设置
    @Autowired
    private SettingsService settingsService;

    @Autowired
    private UserService userService;

    /**
     * 查询通用设置
     *
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {

        SettingsVo settingsVo = settingsService.settings();
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 添加陌生人问题(或者修改问题)
     *
     * @return
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map<String, String> map) {

        String content = map.get("content");
        settingsService.saveQuestion(content);
        return ResponseEntity.ok("问题保存成功");
    }

    /**
     * 添加设置
     *
     * @param settings
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Settings settings) {

        settingsService.saveSettings(settings);
        return ResponseEntity.ok("设置保存成功");
    }

    /**
     * 分页查询黑名单用户列表
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity blackList(
            //获取页码和展示条数，如果没有给默认值
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        //1、调用service
        PageResult pr = settingsService.blackList(page, size);

        return ResponseEntity.ok(pr);
    }

    /**
     * 删除黑名单人员
     *
     * @param blackUserId
     * @return
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable Long blackUserId) {

        settingsService.delete(blackUserId);
        return ResponseEntity.ok("删除黑名单成功");
    }

    /**
     * 修改手机号(发送验证码)
     *
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity send() {

        String mobile = UserHolder.getMobile();
        try {
            //2、发送验证码
            userService.sendCode(mobile);
        } catch (Exception e) {
            //验证码发送失败，抛出自定义异常
            throw new BusinessException(ErrorResult.fail());
        }

        //3、返回数据
        return ResponseEntity.ok("验证码发送成功！");
    }

    /**
     * 修改手机号(校验验证码)
     *
     * @param map
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity check(@RequestBody Map<String, String> map) {

        String verificationCode = map.get("verificationCode");

        Boolean b = userService.check(UserHolder.getMobile(), verificationCode);

        PhoneVo phoneVo = new PhoneVo();
        phoneVo.setVerification(b);
        return ResponseEntity.ok(phoneVo);
    }

    /**
     * 修改手机号(修改数据库)
     *
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity update(@RequestBody Map<String, String> map) {

        User user = new User();
        String phone = map.get("phone");
        user.setId(UserHolder.getUserId());
        user.setMobile(phone);
        userService.update(user);

        return ResponseEntity.ok("手机号修改成功");
    }
}
